home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / newsgroups / misc.20031118-20041115 / 000289_thucdat@hotmail.com_Mon May 10 09:29:55 2004.msg < prev    next >
Internet Message Format  |  2020-01-01  |  5KB

  1. Path: newsmaster.cc.columbia.edu!panix!news.maxwell.syr.edu!postnews1.google.com!not-for-mail
  2. From: thucdat@hotmail.com (Dat Nguyen)
  3. Newsgroups: comp.protocols.kermit.misc
  4. Subject: Logic Programming in C-Kermit
  5. Date: 9 May 2004 16:59:18 -0700
  6. Organization: http://groups.google.com
  7. Lines: 143
  8. Message-ID: <6b1f50ac.0405091559.58c4b822@posting.google.com>
  9. NNTP-Posting-Host: 24.118.27.71
  10. Content-Type: text/plain; charset=ISO-8859-1
  11. Content-Transfer-Encoding: 8bit
  12. X-Trace: posting.google.com 1084147158 30628 127.0.0.1 (9 May 2004 23:59:18 GMT)
  13. X-Complaints-To: groups-abuse@google.com
  14. NNTP-Posting-Date: Sun, 9 May 2004 23:59:18 +0000 (UTC)
  15. Xref: newsmaster.cc.columbia.edu comp.protocols.kermit.misc:15010
  16.  
  17. After a few rounds of Object-Oriented Programming in C-Kermit, I hope you've
  18. seen that C-Kermit can do OOP to solve some tough problems. I am about to embark
  19. on Patterns, an advanced software subject, when I tumble on the following puzzle
  20. in a Prolog book (Prolog and Lisp are considered as the two big tools to
  21. research artificial intelligence):
  22.  
  23. ===============================================================================
  24. Implement an inference engine that can handle symmetric 2-place predicates
  25. (those whose arguments can be interchanged) without looping. Put the functor
  26. symmetric in front of each fact that is to have the symmetric property, and let
  27. symmetric be a prefix operator. For example:
  28.  
  29. symmetric married(john,mary).
  30. symmetric married(melody,michael).
  31.  
  32. ?- prove(married(mary,john)).
  33. yes
  34.  
  35. ?- prove(married(john,mary)).
  36. yes
  37.  
  38.  
  39. ?- prove(married(michael,Who)).
  40. Who = melody
  41.  
  42. ?- prove(married(abc,xyz)).
  43. no
  44. ===============================================================================
  45.  
  46.  
  47.  
  48. Following is my implementation in C-Kermit:
  49.  
  50. ###############################################################################
  51. define symmetric {
  52.     if > \find({\%2},\m(\%1.contain)) 0 {
  53.         echo Relation \%1 \%2 already defined
  54.         return 1
  55.     }
  56.     local \&z[]
  57.     void \fsplit(\%2,&z,{,})
  58.     if > \farraylook(who,&z) -1 {
  59.         echo Cannot relate with who
  60.         return 0
  61.     }
  62.     _define \%1 1
  63.     _asg \%1.contain \m(\%1.contain)|\%2|
  64.     return 1
  65. }
  66.  
  67. define prove {
  68.     if not define \m(\%1) {
  69.         echo Relation \%1 does not exist
  70.         return 0
  71.     }
  72.     local \%n \&w[] \&z[]
  73.     asg \%n \fsplit(\m(\%1.contain),&w,|)
  74.     void \fsplit(\%2,&z,{,})
  75.     if > \farraylook(\%2,&w) -1 {
  76.         echo Yes
  77.         return 1
  78.     } else if > \farraylook({\&z[2],\&z[1]},&w) -1 {
  79.         echo Yes
  80.         return 1
  81.     } else if > \find(\&z[2],who) 0 {
  82.         local \%f \%i
  83.         asg \%f 0
  84.         for \%i 1 \%n 1 {
  85.             if > \find(\&z[1],\&w[\%i]) 0 {
  86.                 echo Who = \freplace(\freplace(\&w[\%i],\&z[1]),{,})
  87.                 asg \%f 1
  88.             }
  89.         }
  90.         return \%f
  91.     } else {
  92.         echo No
  93.         return 0
  94.     }
  95. }
  96.  
  97. ###############################################################################
  98.  
  99. # In a typical soccer tournament, the following matches are usual:
  100. symmetric playsoccer Brazil,Italy
  101. symmetric playsoccer Germany,Italy
  102. symmetric playsoccer England,Brazil
  103. symmetric playsoccer England,Germany
  104. symmetric playsoccer Italy,Germany
  105. symmetric playsoccer Holland,Germany
  106. symmetric playsoccer France,Italy
  107. symmetric playsoccer England,France
  108.  
  109. # In a chess tournament, the following encounters are possible:
  110. symmetric playchess Karpov,Kasparov
  111. symmetric playchess Leko,Kasparov
  112. symmetric playchess Leko,Kramnik
  113. symmetric playchess Karpov,Kramnik
  114. symmetric playchess Annand,Kramnik
  115. symmetric playchess Leko,Karpov
  116. symmetric playchess Ponomariov,Karpov
  117. symmetric playchess Polga,Karpov
  118. symmetric playchess Leko,Polga
  119.  
  120. # In a party, the following pairs would happen:
  121. symmetric danceWith Jane,Robert
  122. symmetric danceWith Jennifer,Mark
  123. symmetric danceWith Mary,John
  124. symmetric danceWith Mary,Robert
  125. symmetric danceWith Suzie,George
  126. symmetric danceWith Kevin,Carol
  127. symmetric danceWith Mark,Carol
  128.  
  129. # All relations playsoccer, playchess, and danceWith can live in the same
  130. environment.
  131. # To see if France play Germany:
  132.  
  133. prove playsoccer France,Germany
  134.  
  135. # To see which teams Brazil plays:
  136.  
  137. prove playsoccer Brazil,Who
  138.  
  139. # To see if Brazil plays Belgium:
  140.  
  141. prove playsoccer Brazil,Belgium
  142.  
  143. # To see if Mark kissed any girl in the party:
  144.  
  145. prove kiss Mark,Who
  146.  
  147. # You'll find the relation kiss hasn't been defined, that is nobody has kissed
  148. anybody. Playboy is a rare animal.
  149.  
  150. As you see, everything, be it Expert system or AI, is only the fakes happening
  151. on the surface. Below the surface is just the handling of bits and bytes.
  152.  
  153. With this, I've just entered the world of logic programming in C-Kermit. Stay
  154. tuned, many exciting things are going to happen here.
  155.  
  156. The script is also found at:
  157. ftp://kermit.columbia.edu/kermit/scripts/ckermit/symmetric
  158.  
  159. Dat